/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.example.training.action;

import com.example.training.persistence.Testcenter;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;


/**
 *
 * @author AkshatJ
 */
@Name("testCenterAction")
@Scope(ScopeType.PAGE)
public class TestCenterAction implements Serializable
{
    
    private Testcenter testCenter;

    @Logger
    private Log log;
    
    @In(value="em")
    private EntityManager em;

    @Create
    public void initialize(){
        testCenter = new Testcenter();
    }

    public String updateTestCenter()
    {
        List existing = em.createQuery("select centerid from Testcenter where centerid=:centerid")
         .setParameter("centerid",testCenter.getCenterid())
         .getResultList();

          if (existing.size()==0)
          {
             em.persist(testCenter);
             log.info("Registered new user #{testCenter.centerid}");
             return "/hello.xhtml";
          }
          else
          {
             FacesMessages.instance().add("TestCenter #{testCenter.centerid} already exists");
             return null;
          }
    }

    public Testcenter getTestCenter() {
        return testCenter;
    }

    public void setTestCenter(Testcenter tcenter) {
        this.testCenter = tcenter;
    }

  


}
